#!/bin/bash

service::get_dirs() {
  # echo $ZMICRO_SERVICES_PATH | awk -F ':' '{out=""; for(i=1;i<=NF;i++){out=out" "$i}; print out}'

  # echo "override: ZMICRO_SERVICES_PATH=$ZMICRO_SERVICES_PATH"
  string::split : $ZMICRO_SERVICES_PATH
}

service::get_path() {
  local service_name=$1
  local avaliable_service_dirs=$(service::get_dirs)

  for service_dir in $avaliable_service_dirs; do
    log::debug "[$(timestamp)][service] try service dir: $service_dir"
    local service_path=$service_dir/$service_name
    if [ -d $service_path ]; then
      log::debug "[$(timestamp)]$(color::success "[service] found service($service_name) in $service_dir")"
      echo $service_path
      return 0
    fi
  done
}

service::list() {
  local avaliable_service_dirs=$(service::get_dirs)
  log "List all available services"
  log ""

  for service_dir in $avaliable_service_dirs; do
    # local namespace=$(echo $service_dir | awk -F '/' '{print $(NF-0)}')
    local namespace=$(string::split_last_element '/' $service_dir)
    if [ ! -d $service_dir ]; then
      log "  namespace: $namespace ($service_dir)"
      log "    (no avaliable services)"
      log ""
      continue
    fi

    local service_names=$(ls $service_dir)
    # local service_count=$(echo $service_names | wc -w)
    local service_count=0
    for service_name in $service_names; do
      local service_path=$service_dir/$service_name

      # Check is a real service
      if [ -f $service_path/docker-compose.yml ]; then
        service_count=$((service_count + 1))
      fi
    done

    log "  namespace: $namespace (total: $service_count) ($service_dir)"
    local index=0
    for service_name in $service_names; do
      local service_path=$service_dir/$service_name

      # Check is a real service
      if [ -f $service_path/docker-compose.yml ]; then
        index=$(number::step index)
        log "    $(color::success "$index $service_name")"
      fi
    done

    if [ $service_count -eq 0 ]; then
      log "    (no avaliable services)"
    fi

    log ""
  done
}

service::run() {
  local service_name=$1
  local commands=${@:2}

  log::debug "[$(timestamp)][service] ZMICRO_SERVICES_PATH: $ZMICRO_SERVICES_PATH"
  log::debug "[$(timestamp)][service] service name: $service_name"
  log::debug "[$(timestamp)][service] service command: $commands"

  local service_path=$(service::get_path $service_name)
  if [ "$service_path" = "" ]; then
    log::error "[$(timestamp)][service] service($service_name) not found"
    exit 1
  fi

  log::debug ""
  cd $service_path
  docker-compose -f docker-compose.yml $commands
}

service::get_config() {
  local service_name=$1

  local service_path=$(service::get_path $service_name)
  if [ "$service_path" = "" ]; then
    log::error "[$(timestamp)][service] service($service_name) not found"
    exit 1
  fi

  local service_config=$service_path/docker-compose.yml
  if [ ! -f $service_config ]; then
    log::error "[$(timestamp)][service] service($service_name) config(docker-compose.yml) not found"
  fi

  echo $service_config
}

service::detect_path() {
  local custom_service_path=$(zmicro config get ZMICRO_SERVICES_PATH)
  if [ "$custom_service_path" = "" ]; then
    log::error "Please your service path first."
    log "Run the following command to config:"
    log ""
    log "$ $(color::success "zmicro config service <service_path>")"
    log ""

    exit 1
  fi
}

export -f service::get_dirs
export -f service::get_path

export -f service::run
export -f service::list
export -f service::get_config

export -f service::detect_path
